package com.example.blew.demo.Controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.blew.demo.model.LoginUserDto;
import com.example.blew.demo.model.User;
import com.example.blew.demo.service.UserServce;
import com.example.blew.demo.uitl.JwtUtil;
import com.example.blew.demo.uitl.RrturnMesage;
import com.example.blew.demo.uitl.UUIDTOLong;
import com.example.blew.omp.system.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author chaodong.zeng@pcitc.com
 * @since 2022/9/12
 */
@RestController
@RequestMapping("user12312")
@Api(tags = "user")
public class UserControllertest {

    @Resource
    UserServce userServce;

    @Resource
    private JwtUtil jwtUtil;

    @Autowired
    RedisUtil redisUtil;


    @CrossOrigin
    @PostMapping("log32in")
    @ApiOperation(value = "UserLogin")
    public RrturnMesage login(@RequestBody LoginUserDto loginUserDto, HttpServletRequest request) {
        //验证码校验
        HttpSession session = request.getSession();
        String code=loginUserDto.getCode();
        boolean checkCode = checkCode(code, session);

        if(!checkCode){
            return RrturnMesage.error().message("验证码错误").code(1001);
        }
        //登录验证
        User user=new User();
        user.setUserName(loginUserDto.getUserName());
        user.setPassword(loginUserDto.getPassword());

        User loginUser = userServce.login(user);
        HashMap<String, Object> map = new HashMap<>();
        if (ObjectUtil.isNotEmpty(loginUser)) {
            //登录通过添加uuid存redis，添加时间
            String UserUUID = UUIDTOLong.getUUID();
            HashMap<String, String> plMap = new HashMap<>();
            plMap.put("username", loginUser.getUserName());
            String token = jwtUtil.getToken(plMap);
            //通过验证，将相关用户信息及token等存入map，用于返回
            map.put("state", true);
            map.put("msg", "认证成功");
            map.put("token", token);
        }
        return RrturnMesage.ok().data("ok", map);
    }


    @CrossOrigin
    @PostMapping("login2")
    @ApiOperation(value = "UserLogin2")
    public RrturnMesage login2(@RequestBody LoginUserDto loginUserDto, HttpServletRequest request) {
        //登录验证
        User user=new User();
        user.setUserName(loginUserDto.getUserName());
        user.setPassword(loginUserDto.getPassword());
        User loginUser = userServce.login(user);
        HashMap<String, Object> map = new HashMap<>();
        if (ObjectUtil.isNotEmpty(loginUser)) {
            //登录通过添加uuid存redis，添加时间
            String UserUUID = UUIDTOLong.getUUID();
            HashMap<String, String> plMap = new HashMap<>();
            plMap.put("username", loginUser.getUserName());
            String token = jwtUtil.getToken(plMap);
            //通过验证，将相关用户信息及token等存入map，用于返回
            map.put("state", true);
            map.put("msg", "认证成功");
            map.put("token", token);
        }
        return RrturnMesage.ok().data("ok", map);
    }


    /***
     * 验证码校验
     * @param code
     * @param session
     * @return
     */
    public boolean checkCode(String code, HttpSession session) {
        String key = session.getId();
        Object read = redisUtil.read(key);
        String str=(String)read;
        if(StrUtil.hasEmpty(str))
        {
            return  false;
        }else if(StrUtil.equals(str,code)){
            return true;
        }
        return false;
    }

    @ApiOperation(value = "getCaptcha")
    @GetMapping("getCaptcha")
    public void getCaptcha(HttpServletResponse response, HttpServletRequest request) {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(130, 60, 4, 4);

        try {
            captcha.write(response.getOutputStream());
            HttpSession session = request.getSession();
            // 存redis
            redisUtil.write(session.getId(),captcha.getCode());
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }



}